// pages/community/wenda/editor.js
var shetuan = require('../shetuan.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type :3,
    shetuan:{},
    uploadimages:[],
      isbaocun:0
  },

  //填装数据
  formdata:function(e){
    var that = this;
    shetuan.formData(e.currentTarget.dataset.name,e.detail.value);
    this.setData({
      shetuan:shetuan.shetuan
    })
  },
  push:function(e){
      var that = this;
    // console.log(this.data)
      if(!this.data.shetuan.title || ''==this.data.shetuan.title){
          wx.showToast({
              title:'请填写标题',
              icon: 'none',
              duration: 2000
          })
          return;
      }
      if(!this.data.shetuan.content || ''==this.data.shetuan.content){
          wx.showToast({
              title:'请填写内容',
              icon: 'none',
              duration: 2000
          })
          return;
      }
      that.setData({
          isbaocun:1
      })
    shetuan.push(this.data);
      setTimeout(function(){
          wx.navigateBack(1);
      },2000)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    getApp().globalData.uploadimages = [];
    getApp().globalData.isWanChengUp = 0;
    this.setData({
        shetuan:{},
        uploadimages:[],
    })
      shetuan.clearData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      var that = this;
      shetuan.type = 3;
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  uploadimages:function(e){
    var that = this;
    // that.setData({
    //     uploadimages:[]
    // })
    if(getApp().globalData.uploadimages.length>=6){
      wx.showModal({
        title:'提示',
        content:'最多上传6张图片'
      })
    }else{
      shetuan.chooseimages();
      var num = 0;
     
      var i = setInterval(function(){
        var globallength = getApp().globalData.uploadimages.length;
        var uplength = that.data.uploadimages.length;
        // console.log(that.data.uploadimages.length + "----------" + (num++));
        if(uplength >= 0 && globallength > uplength){
          that.setData({
            uploadimages:getApp().globalData.uploadimages
          })
          clearInterval(i);
        }
      },1000)
    }
    // console.log('editor....'+JSON.stringify(shetuan.shetuan))
      // wx.showLoading({
      //   title:'上传中。。。'
      // })
      // setTimeout(function(){
      //     if(1 == getApp().globalData.isWanChengUp){
      //         that.setData({
      //             uploadimages:getApp().globalData.uploadimages
      //         })
      //     }
      //     wx.hideLoading();
      // },2000)
  },//删除图片
    delimage:function(e){
        var indexold = e.currentTarget.dataset.index;
        var that = this;
        // console.log('index==='+e.currentTarget.dataset.index);
        var upimages = that.data.uploadimages;
        var newUpimage = [];
        upimages.forEach(function(item,index){
            // console.log(JSON.stringify(item)+'----------'+index);
            if(parseInt(indexold) != parseInt(index)){
                newUpimage.push(item);
            }
        })
        that.setData({
            uploadimages:newUpimage
        })
        getApp().globalData.uploadimages=newUpimage;
    }
})
